<%--
  Created by IntelliJ IDEA.
  User: wangfei
  Date: 2021/4/17
  Time: 11:41 上午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(function ()
        {
            $("#ok").click(function ()
            {
                $.ajax({
                    url:"test11",
                    headers:
                        {
                            "Authorization":"Bearer "+sessionStorage.getItem("token")
                        }
                })
            })

            $("#no").click(function ()
            {
                $.ajax({
                    url:"user/login",
                    dataType:"json",
                    success:function(data)
                    {
                        sessionStorage.setItem("token",data.data.token)
                    },
                    error:function (data)
                    {
                        alert("请求失败")
                    }
                })
            })

            $("#test").click(function ()
            {
                $.ajax({
                    method:"post",
                    url:"aaa",
                    data:JSON.stringify({
                        "userId":"214124124124",
                        "username":"12131134"
                    }),
                    contentType: "application/json;charset=utf-8",
                    dataType: "application/json;charset=utf-8",
                    headers:
                        {
                            "Authorization":"Bearer "+sessionStorage.getItem("token")
                        },
                })
            })

            $("#do").click(function ()
            {
                $.ajax({
                    url:"uploadPic",
                    data:
                        {
                            "pid":1
                        },
                    headers:
                        {
                            "Authorization":"Bearer "+sessionStorage.getItem("token")
                        },
                })
            })
        })
    </script>

    <script>
        function uploadPhoto() {
            $("#photoFile").click();
        }

        /**
         * 上传图片
         */
        function upload() {
            if ($("#photoFile").val() == '') {
                return;
            }
            var formData = new FormData();
            formData.append('file', document.getElementById('photoFile').files[0]);
            $.ajax({
                url:"${pageContext.request.contextPath}/upload",
                type:"post",
                data: formData,
                headers:
                    {
                        "Authorization":"Bearer "+sessionStorage.getItem("token")
                    },
                contentType: false,
                processData: false,
                success: function(data) {
                    if (data.type == "success") {
                        $("#preview_photo").attr("src", data.filepath+data.filename);
                        $("#productImg").val(data.filename);
                    } else {
                        alert(data.msg);
                    }
                },
                error:function(data) {
                    alert("上传失败")
                }
            });
        }
    </script>
    <script>
        $(function ()
        {
            $("#test11").click(function ()
            {
                $.ajax({
                    method: "DELETE",
                    url:"users/",
                    data:{
                        beSupportedUserId:1,
                    } ,
                    contentType:"application/json;charset=utf-8",
                })
            })
        })
    </script>
</head>
<body>
<button id="ok">点击</button>
<button id="no">登录</button>
<button id="test">测试</button>
<button id="do">测试</button>
<br>
<a href="javascript:void(0)" onclick="uploadPhoto()">选择图片</a>
<input type="file" id="photoFile" style="display: none;" onchange="upload()">
<img id="preview_photo" src="" width="200px" height="200px">

<button id="test11">点击</button>
</div>
</body>
</html>
